var chargingStationData = [];
var chargingStationMap = undefined;
var markers = [];
var selectPosition = [];

$(document).ready(function() {
    showLoading();
    $('title', parent.document).html($('title').text());
    setBaseUrl();
    addTitle("chargingStationDetails");
    search();
});

function search(){
	$('#ChargingStationID').val($("#detailID", parent.document).val());
	$ajax('searchChargingStation', 'searchChargingStationDetail', searchChargingStation);
	$("#detailID", parent.document).val("");
}
function searchChargingStation(data) {
	chargingStationData = data && data.chargingstationList || [];
	createDetailUlBody("detailOneBody", chargingStationDetailTable, chargingStationData[0], 1);
	createDetailUlBody("detailTwoBody", chargingStationDetailTable, chargingStationData[0], 2);
	createDetailUlBody("detailThreeBody", chargingStationDetailTable, chargingStationData[0], 3);
	createDetailUlBody("detailFourBody", chargingStationDetailTable, chargingStationData[0], 4);
	createDetailUlBody("detailFireBody", chargingStationDetailTable, chargingStationData[0], 5);
	createDetailUlBody("detailSixBody", chargingStationDetailTable, chargingStationData[0], 6);
	createDetailUlBody("detailSevenBody", chargingStationDetailTable, chargingStationData[0], 7);
	createDetailUlBody("detailEightBody", chargingStationDetailTable, chargingStationData[0], 8);
	$("#stationName").html('');
	$("#stationName").html(chargingStationData[0].chargingStationName);
	$("#bgEquipmentCount").html('<span class="grayWord">大车桩数</span>: <a class="details" onclick=\'pass("'+chargingStationData[0].chargingStationID+'")\'>'+chargingStationData[0].bgEquipmentCount+'</a>'); 
	setFormDisabled("saveForm");
	$('#confirm').hide();
	$('#confirmMap').hide();
	$("#longitude").attr("readonly", true);
	$("#latitude").attr("readonly", true);
	if (!chargingStationMap) {
		$.getScript("https://webapi.amap.com/maps?v=1.3&key=244bc2cb511336b2a3e5129ad0309a13&&plugin=AMap.OverView,AMap.MapType,AMap.Geolocation,AMap.Scale,AMap.ToolBar,AMap.Autocomplete,AMap.PlaceSearch&callback=tableMap");
	} else {
		tableMap();
	}
	changeImage(0);
	setTimeout(hideLoading, 500);
}
function pass(value){
	window.parent.passVal(value, "passValue", "chargingEquipment/equipmentInfo", "drownDown2", "", "equipmentPage", "equipmentInfo");
}
function tableMap(){
	var longitude = $("#longitude").text();
	var latitude = $("#latitude").text();
	if (longitude && latitude) {
		var centerPosition = [];
		centerPosition.push(longitude);
		centerPosition.push(Number(latitude) + 0.015);
		var position = [];
		position.push(longitude);
		position.push(latitude);
	}
	chargingStationMap = setMap(centerPosition, 'tableContainer', 10);
	addMarker(position, "static/images/chargingStationMapsm.png");
}
function confirmMap(){
	$("#map").modal("hide");
	if (selectPosition && selectPosition.length > 0) {
		$("#longitude").text(selectPosition[0]);
		$("#latitude").text(selectPosition[1]);
	}
	tableMap();
}
function confirm(){
	return checkForm("saveForm", chargingStationTable);
}

function changeGPS() {
	$("#map").modal("show");
	var longitude = $("#longitude").text();
	var latitude = $("#latitude").text();
	if (longitude && latitude) {
		var position = [];
		position.push(longitude);
		position.push(latitude);
	}
	chargingStationMap = setMap(position);
	AMap.plugin(['AMap.Autocomplete','AMap.PlaceSearch'],function(){
		var autoOptions = {
			city: '', //城市，默认全国  
	        input: "keyword"//使用联想输入的input的id
	    };
	    autocomplete= new AMap.Autocomplete(autoOptions);
		var placeSearch = new AMap.PlaceSearch({
			city: '', //城市，默认全国  
	        map: chargingStationMap
	    });
	    AMap.event.addListener(autocomplete, "select", function(e){
	        placeSearch.search(e.poi.name);
	    });
	});
	AMap.event.addListener(chargingStationMap, 'click', getLnglat);
	addMarker(position, "static/images/chargingStationMap.png");
}
function getLnglat(e){
	selectPosition = [];
	selectPosition.push(e && e.lnglat && e.lnglat.getLng() || 0);
	selectPosition.push(e && e.lnglat && e.lnglat.getLat() || 0);
	addMarker(selectPosition, "static/images/chargingStationMap.png");
}
function addMarker(position, icon){
	if (position && position.length > 0) {
		chargingStationMap.remove(markers);
		markers = [];
		marker = new AMap.Marker({
			icon: icon,
	        map: chargingStationMap,
	        position: position,
	        animation: "AMAP_ANIMATION_DROP"
	    });
		markers.push(marker);
		marker = null;
	}
}

function changeImage(index) {
	$("#imageForm").empty();
	$("#imageForm").append('<input id="chargingStationImage" name="chargingStationImage" type="file" multiple class="file-loading"/>');
	if (index != undefined) {
		var imageUrl = "getChargingStationImage";
		var imageNameList = chargingStationData && chargingStationData[index] && chargingStationData[index].chargingStationImageName || [];
		var length = imageNameList.length;
		chargingStationData[index].initialPreview = [];
		chargingStationData[index].initialPreviewConfig = [];
		for (var i=0; i<length; i++) {
			chargingStationData[index].initialPreview.push('<img src="' + imageUrl + '?subfolder=' + chargingStationData[index].chargingStationID + '&&fileName=' + imageNameList[i] + '" style="width:220px;height:110px;" />');
			chargingStationData[index].initialPreviewConfig.push({url: "deleteChargingStationImage", extra: {fileName: imageNameList[i], subfolder: chargingStationData[index].chargingStationID}});
		}
	}
	var showBrowse = true;
	var showUpload = true;
	chargingStationData[index].initialPreviewConfig = [];
	showBrowse = false;
	showUpload = false;
	var maxFileCount = 5 - (chargingStationData && chargingStationData[index] && chargingStationData[index].chargingStationImageName && chargingStationData[index].chargingStationImageName.length || 0)
	var option = {
		uploadUrl: "saveChargingStationImage",
		allowedFileExtensions: ['jpg', 'png','gif'], 
		showUpload: showUpload, 
		showCaption: false, 
		showClose: false, 
		overwriteInitial: false, 
		showKvUpload: false, 
		showErrorPreview: false, 
		uploadExtraData: {subfolder: chargingStationData && chargingStationData[index] && chargingStationData[index].chargingStationID}, 
		maxFileCount: maxFileCount,
		enctype: 'multipart/form-data',
		initialPreview: chargingStationData && chargingStationData[index] && chargingStationData[index].initialPreview,
		previewSettings: {image: {width: "220px", height: "100px"}},
		initialPreviewConfig: chargingStationData && chargingStationData[index] && chargingStationData[index].initialPreviewConfig,
		showBrowse: showBrowse,
		index: index
	};
	selectFile("chargingStationImage", option, resetPreview);
}
function goback() {
	window.parent.skipMenu('chargingStation/chargingStation', 'drownDown1', '', 'chargingStationPage', 'chargingStation');
}
function resetPreview(data) {
	if (data) {
		var index = data.index;
		var imageNameList = chargingStationData && chargingStationData[index] && chargingStationData[index].chargingStationImageName || [];
		var file = data.file;
		if (data.flag == "delete" && file) {
			var imgIndex = imageNameList.indexOf(file);
			chargingStationData[index].chargingStationImageName.splice(imgIndex, 1);
			changeImage(index);
		} else if (data.flag == "uploaded" && file) {
			var length = file.length;
			for (var i=0; i<length; i++) {
				chargingStationData[index].chargingStationImageName.push(file[i].name);
			}
			changeImage(index);
		}
	}
}